package com.ljx.itbbs.server.mall.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.itbbs.server.article.domain.entity.ArticleEntity;
import com.ljx.itbbs.server.mall.domain.entity.OrderEntity;
import com.ljx.itbbs.server.mall.domain.vo.req.OrderReq;
import com.ljx.itbbs.server.mall.mapper.OrderMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class OrderDao extends ServiceImpl<OrderMapper, OrderEntity> {

    public OrderEntity gerOrderByUIdAndObjId(Long userId, String resourceId) {
        return lambdaQuery().eq(OrderEntity::getUserId, userId)
                .eq(OrderEntity::getObjId, resourceId)
                .one();
    }

    public OrderEntity gerOrderById(Long orderId) {
        return lambdaQuery().eq(OrderEntity::getId, orderId).one();
    }

    public List<OrderEntity> batchOrderByUIdAndObjIds(Long userId, List<String> resIds) {
        return lambdaQuery().eq(OrderEntity::getUserId, userId)
                .in(OrderEntity::getObjId, resIds)
                .list();
    }

    public OrderEntity getByObjId(String finalObjId) {
        return lambdaQuery().eq(OrderEntity::getObjId, finalObjId).one();
    }

    public IPage<OrderEntity> getOrderPage(Long userId, OrderReq req) {
        IPage<OrderEntity> page = new Page<>(req.getCurrPage(), req.getPageSize());
        // 条件判断
        LambdaQueryWrapper<OrderEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderEntity::getUserId, userId);

        return this.baseMapper.selectPage(page, wrapper);
    }

    public void updatePaymentStatus(Long orderId, Integer type) {
        lambdaUpdate().set(OrderEntity::getPaymentStatus, type)
                .eq(OrderEntity::getId, orderId)
                .update();
    }
}
